﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kassasysteem_TacoTruijens
{
    public partial class tickets : System.Web.UI.Page
    {
        private DataClassesDataContext Database = new DataClassesDataContext();

        protected void Page_Load(object sender, EventArgs e)
        {
            addHeaderRowToTableTickets();
            fillTableTickets();
        }

        private void fillTableTickets()
        {
            var tickets = (from t in Database.Tickets
                          orderby t.nummer descending
                          select t).Take(30);

            String ticketNumber = Request.QueryString["ticketnumber"];

            if (ticketNumber != null)
            {
                tickets = null;
                tickets = from t in Database.Tickets
                          where t.nummer == Convert.ToInt32(ticketNumber)   
                          orderby t.nummer descending
                          select t;

                labelSearch.Text = "U heeft gezocht op kaartnummer " + ticketNumber + ".";
            }
            
            Boolean even = false;

            foreach (Ticket t in tickets)
            {
                TableRow ticketTableRow = new TableRow();

                if (even)
                {
                    ticketTableRow.CssClass = "even";
                    even = false;
                }
                else
                {
                    even = true;
                }

                TableCell numberTableCell = new TableCell();
                TableCell typeTableCell = new TableCell();
                TableCell showTableCell = new TableCell();
                TableCell cancelTableCell = new TableCell();

                cancelTableCell.CssClass = "alignCenter";

                numberTableCell.Text = t.nummer.ToString();
                typeTableCell.Text = t.tickettype_naam;
                showTableCell.Text = t.Voorstelling.datumtijd.ToString() + ": " + t.Voorstelling.Film.naam;

                if (t.voorstelling_datumtijd >= DateTime.Now.AddHours(2))
                {
                    cancelTableCell.Text = "<a href='cancelticket.aspx?ticketnumber=" + t.nummer + "'><img src='images/delete.png' alt='Annuleer' title='Annuleer ticket' /></a>";            
                }
                else
                {
                    cancelTableCell.CssClass = "toolate";
                    cancelTableCell.Text = "Kan niet meer annuleren";
                }              

                ticketTableRow.Cells.Add(numberTableCell);
                ticketTableRow.Cells.Add(typeTableCell);
                ticketTableRow.Cells.Add(showTableCell);
                ticketTableRow.Cells.Add(cancelTableCell);

                tableTickets.Rows.Add(ticketTableRow);
            }
        }

        private void addHeaderRowToTableTickets()
        {
            TableRow headerTableRow = new TableRow();
            headerTableRow.CssClass = "header";

            TableCell numberTableCell = new TableCell();
            TableCell typeTableCell = new TableCell();
            TableCell showTableCell = new TableCell();
            TableCell cancelTableCell = new TableCell();

            numberTableCell.Text = "Kaartnummer";
            typeTableCell.Text = "Type";
            showTableCell.Text = "Voorstelling";
            cancelTableCell.Text = "Annuleer";

            headerTableRow.Cells.Add(numberTableCell);
            headerTableRow.Cells.Add(typeTableCell);
            headerTableRow.Cells.Add(showTableCell);
            headerTableRow.Cells.Add(cancelTableCell);

            tableTickets.Rows.Add(headerTableRow);
        }

        protected void buttonSearch_Click1(object sender, EventArgs e)
        {
            String ticketNumber = textBoxNumber.Text;

            if (ticketNumber.Length > 0)
            {
                String queryString = "tickets.aspx?ticketnumber=" + ticketNumber;
                Server.Transfer(queryString);
            }
        }
    }
}